package com.xcx.eduservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import com.xcx.eduservice.entity.aclentity.Company;
import com.xcx.eduservice.mapper.CompanyMapper;
import com.xcx.eduservice.request.aclrequest.companyrequest.CompanyRequest;
import com.xcx.eduservice.request.aclrequest.companyrequest.GetCoByNameRequest;
import com.xcx.eduservice.service.CompanyService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Primary
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    final public CompanyMapper companyMapper;


    /**
     * 新增团队，公司
     */


    @Override
    public ReturnMessage<Object> addCompany(CompanyRequest respVo) {
        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
        companyQueryWrapper.eq("company", respVo.getCompany());
        Company selectOne = companyMapper.selectOne(companyQueryWrapper);
        if (selectOne != null) {
            ReturnMessageUtils.errorWithEum(ErrorEnum.E_GROUP_CODE_EXIST);
        }


        Company company = new Company();
        BeanUtils.copyProperties(respVo, company);
        companyMapper.insert(company);
        return ReturnMessageUtils.success();
    }


    /**
     * 根据团队名称或者性质，查询所有团队
     */
    @Override
    public ReturnMessage<Object> queryCompanyByNL(GetCoByNameRequest respVo) {


        QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(respVo.getCompany())) {
            companyQueryWrapper.like("company", respVo.getCompany());

        }

        if (!StringUtils.isEmpty(respVo.getLineName())) {
            companyQueryWrapper.like("line_name", respVo.getLineName());

        }
        Integer count = companyMapper.selectCount(companyQueryWrapper);

        IPage<Company> page = new Page<>(respVo.getPageNo(), respVo.getPageSize(), count);

        IPage<Company> companyIPage = companyMapper.selectPage(page, companyQueryWrapper);


        return ReturnMessageUtils.success(companyIPage);
    }


}
